﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Graphics;
using System.Linq;


namespace FolderData
{
    public class MapBackground
    {
        public static readonly int TextureWidth = 256;
        public static readonly int TextureHeight = 256;

        int row;
        int col;
        List<String> textureAssetnameList;
        List<Texture2D> textureList;


        public int Row
        {
            get { return row; }
            set { row = value; }
        }
        public int Col
        {
            get { return col; }
            set { col = value; }
        }
        public String TextureAssetnamePrefix { get; set; }
        [ContentSerializerIgnore]
        public List<String> TextureAssetnameList
        {
            get { return textureAssetnameList; }
            set { textureAssetnameList = value; }
        }

        public void LoadContent(ContentManager content)
        {
            textureList = new List<Texture2D>();
            foreach (String s in textureAssetnameList)
                textureList.Add(content.Load<Texture2D>(s));
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color)
        {
            for(int i = 0; i < row; i++)
                for (int j = 0; j < col; j++)
                {
                    spriteBatch.Draw(textureList[col * i + j], position + new Vector2(i * TextureWidth, j * TextureHeight), color);
                }
        }

        public MapBackground()
        {
            textureAssetnameList = new List<string>();
            textureList = new List<Texture2D>();
        }

        #region Processor
        public class MapBackgroundReader : ContentTypeReader<MapBackground>
        {
            protected override MapBackground Read(ContentReader input, MapBackground existingInstance)
            {
                MapBackground bg = new MapBackground();

                bg.row = input.ReadInt32();
                bg.col = input.ReadInt32();
                bg.TextureAssetnamePrefix = input.ReadString();

                bg.TextureAssetnameList = new List<string>();
                bg.textureList = new List<Texture2D>();
                for (int i = 0; i < bg.row * bg.col; i++)
                    bg.TextureAssetnameList.Add(bg.TextureAssetnamePrefix + "_" + i);

                return bg;
            }
        }

        [ContentTypeWriter]
        public class MapBackgroundWriter : ContentTypeWriter<MapBackground>
        {
            protected override void Write(ContentWriter output, MapBackground value)
            {
                output.Write(value.row);
                output.Write(value.col);
                output.Write(value.TextureAssetnamePrefix);
            }
            public override string GetRuntimeReader(TargetPlatform targetPlatform)
            {
                return typeof(MapBackground.MapBackgroundReader).AssemblyQualifiedName;
            }
            public override string GetRuntimeType(TargetPlatform targetPlatform)
            {
                return typeof(MapBackground).AssemblyQualifiedName;
            }
        }
        #endregion
    }
}
